package me.shurufa.activities;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;

import java.util.ArrayList;

import butterknife.Bind;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.net.TencentWebViewClient;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Global;
import me.shurufa.utils.LogUtils;

/**
 * 点击去网站兑换图书跳转页面
 *
 * @FileName: me.shurufa.activities.ExangeActivity.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-19 12:30
 */
@SuppressLint("SetJavaScriptEnabled")
public class ExangeActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.webview)
    WebView webview;

    /**
     *该属性防止网页中其他url加载失败导致的主url加载失败
     */
    public ArrayList<String> urlList = new ArrayList();

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_web_multiplex;
        super.onCreate(savedInstanceState);
        initWebView();
        initUI();
    }

    private void initWebView() {
        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webview.setWebViewClient(new TencentWebViewClient(urlList,webview, null));
//        Js预留接口 样例方法
        webview.addJavascriptInterface(new JavascriptInterface(this), "imagelistner");
        webview.setWebChromeClient(webChromeClient);
        webview.loadUrl("http://www.baidu.com/");
    }

    private void initUI() {
        titleTextView.setText(getString(R.string.exange));
/////////////////////////////////
        if (Global.DEBUG){
            Intent intent = new Intent(Constants.ACTION_POINTS_CHANGE);
            intent.putExtra(Constants.EXTRA_POINTS,1234);
            EventBus.getDefault().post(intent);
        }
//////////////////////////////////
    }

    @Override
    public void onClick(View v) {

    }

    WebChromeClient webChromeClient = new WebChromeClient(){
        @Override
        public void onReceivedTitle(WebView view, String t) {
            super.onReceivedTitle(view, t);
        }
    };


    /**
     * 此方法仅为样例 在webView中点击<img 时支持调用本地java方法
     */
    @SuppressLint("SetJavaScriptEnabled")
    public class JavascriptInterface {

        private Context context;

        public JavascriptInterface(Context context) {
            this.context = context;
        }

        @android.webkit.JavascriptInterface
        public void openImage(String img) {
            //
            if (!TextUtils.isEmpty(img)) {
                LogUtils.kLog().d(img);
            }
        }
    }
}
